package com.kindergarten.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kindergarten.pojo.*;
import com.kindergarten.service.IClassesService;
import com.kindergarten.service.ICourseService;
import com.kindergarten.service.IUserService;
import com.kindergarten.utils.DataResults;
import com.kindergarten.utils.MMGridPageVoBean;
import com.kindergarten.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bruce
 * @since 2022-10-27
 */
@Controller
@RequestMapping("/course-api")
public class CourseController {

    @Autowired
    ICourseService courseService;

    @Autowired
    IClassesService classesService;

    @Autowired
    IUserService iUserService;


    /**
     * 01-跳转到模板引擎页面
     * @return
     */
    @GetMapping("topage")
    public String topage() {
        return "course";
    }

    /**
     * 02-数据加载分页
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("page")
    public MMGridPageVoBean page(Course course) {

        MMGridPageVoBean pageVoBean = new MMGridPageVoBean();
        QueryWrapper<Course> queryWrapper = new QueryWrapper<Course>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("del",0); //删除不查询

        IPage<Course> page = courseService.page(new Page<Course>(course.getPage(), course.getLimit()), queryWrapper);

        List<Course> records = page.getRecords();
        for (Course record : records) {
            String cname = classesService.getById(record.getClassId()).getName();
            record.setCname(cname);
        }
        pageVoBean.setRows(records); //每页数据
        pageVoBean.setTotal(page.getTotal()); //总条数
        return pageVoBean;
    }


    /**
     * 02-跳转到模板引擎页面
     * @return
     */
    @GetMapping("toaddpage")
    public String toaddpage(Model model) {
        List<User> userList = iUserService.list(new QueryWrapper<User>().eq("del", 0).eq("user_state", "2"));
        List<Classes> classesList = classesService.list(new QueryWrapper<Classes>().eq("del", 0));
        model.addAttribute("claObjs",classesList);
        model.addAttribute("usersObjs",userList);
        return "courseAdd";
    }

    /**
     * 新增
     * @return
     */
    @ResponseBody
    @PostMapping("add")
    public DataResults add(Course course){

        String teaId = course.getTeaId();
        User user = iUserService.getById(teaId);
        course.setTeaName(user.getUserName());

        return DataResults.success(ResultCode.SUCCESS,courseService.save(course));
    }

    /**
     * 删除
     *
     * @return
     */
    @ResponseBody
    @DeleteMapping("delete/{id}")
    public DataResults delete(@PathVariable("id") Integer id) {
        Course course = new Course();
        course.setId(id);
        course.setDel(1);
        return DataResults.success(ResultCode.SUCCESS, courseService.updateById(course));
    }

}
